<template>
  <div class="common-layout">
    <div class="table-box">
      <el-table
        :data="tableData"
        style="width: 100%"
        border
        height="100%"
        v-loading="loading"
        element-loading-text="拼命加载中"
      >
        <el-table-column prop="userCode" label="序号"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="age" label="年龄"></el-table-column>
        <el-table-column prop="sex" label="性别"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="phone" label="电话"></el-table-column>
        <el-table-column prop="type" label="用户权限"></el-table-column>
        <el-table-column prop="operation" label="操作">
          <template #default="{ row }">
            <el-button type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="bottom-box">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20, 50]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalItems"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 编辑用户 -->
    <el-dialog
      title="编辑用户"
      v-model="editDialogVisible"
      width="60%"
      :close-on-click-modal="false"
    >
      <el-form label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editForm.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="editForm.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="editForm.sex" placeholder="请选择性别">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="editForm.phone" placeholder="请输入电话"></el-input>
        </el-form-item>
        <el-form-item label="用户权限" prop="type">
          <el-select v-model="editForm.type" placeholder="请选择用户权限">
            <el-option label="超级管理员" value="超级管理员"></el-option>
            <el-option label="管理员" value="管理员"></el-option>
            <el-option label="普通用户" value="普通用户"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref } from 'vue'

const tableData = ref([
  {
    userCode: '1',
    name: '张三',
    age: 18,
    sex: '男',
    email: '123466@qq.com',
    phone: '12345678901',
    type: '超级管理员',
  },
  {
    userCode: '2',
    name: 'wang三',
    age: 18,
    sex: '男',
    email: '123466@qq.com',
    phone: '12345678901',
    type: '管理员',
  },
  {
    userCode: '3',
    name: 'li三',
    age: 18,
    sex: '男',
    email: '123466@qq.com',
    phone: '12345678901',
    type: '管理员',
  },
])

const loading = ref(false)
const editDialogVisible = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const totalItems = ref(tableData.value.length)
const size = ref('medium')
const disabled = ref(false)
const background = ref(true)

const editForm = ref({
  name: '',
  age: '',
  sex: '',
  email: '',
  phone: '',
  type: '',
})

const handleSizeChange = (newSize) => {
  pageSize.value = newSize
  // 这里可以添加逻辑来重新获取数据
}

const handleCurrentChange = (newPage) => {
  currentPage.value = newPage
  // 这里可以添加逻辑来重新获取数据
}

const handleEdit = (row) => {
  editDialogVisible.value = true
  editForm.value = { ...row }
}

const handleDelete = (row) => {
  console.log(row)
}

const submitEdit = () => {
  console.log('submitEdit', editForm.value)
}
</script>
<style scoped>
.common-layout {
  padding: 20px;
  overflow: auto;
}
.table-box {
  margin-right: 20px;
  margin-top: 20px;
  height: 780px;
  overflow: auto;
}
.bottom-box {
  margin-right: 20px;
  margin-top: 20px;
  display: flex;
  justify-content: right;
}
</style>
